// isIdCard
import { isPhone, isUnifyTheSCC, validateIsemail } from '@fl/utils/validate'
import { reactive } from 'vue'

export function useRule() {
    const validatorphonenumber = (rule, value, callback) => {
        if (!isPhone(value))
            callback(new Error('请输入正确的手机号'))
        else callback()
    }

    // const validatorregisteredMobile = (rule, value, callback) => {
    //     if (!value)
    //         callback(new Error('请输入正确的企业联系电话'))

    //     callback()
    // }

    const validatorunifiedSocialCreditCode = (rule, value, callback) => {
        if (!isUnifyTheSCC(value))
            callback(new Error('请输入正确的统一社会信用代码'))
        else callback()
    }
    const rules = reactive<any>({
        catTypeId: [
            { message: '请选择', required: true, trigger: 'blur' },
        ],
        email: [
            { required: false, trigger: 'blur', validator: validateIsemail },
        ],
        areaCode: [
            { message: '请选择', required: true, trigger: 'blur,change' },
        ],
        enterpriseLevelId: [
            { message: '请选择', required: true, trigger: 'blur' },
        ],
        enterpriseName: [
            { message: '请输入企业名称', required: true, trigger: 'blur' },
        ],
        // legalCardBack: [
        //     { message: '请上传图片', required: true },
        // ],
        // legalCardFront: [
        //     { message: '请上传图片', required: true },
        // ],
        // parentId: [
        //     { message: '请选择上级', required: true, trigger: 'change' },
        // ],
        phonenumber: [
            { required: true, trigger: 'blur', validator: validatorphonenumber },
        ],
        // registeredMobile: [
        //     { message: '请填写联系电话', required: true, trigger: 'blur' },
        // ],
        unifiedSocialCreditCode: [
            { required: true, trigger: 'blur', validator: validatorunifiedSocialCreditCode },
        ],
        userName: [
            { message: '请输入管理员姓名', required: true, trigger: 'blur' },
        ],
    })

    // TODO：手机号 企业名称 唯一性校验
    const isOnly = (key: string, val: string) => {
        const cache = window.sessionStorage.getItem('CACHE_phonenumber_enterpriseName')
        const target: any = cache ? JSON.parse(cache) : ''
        return !target[key].includes(val)
    }

    const user = window.localStorage.getItem('user')
    const denglm = user ? JSON.parse(user).userInfo.code : ''

    return { denglm, isOnly, rules }
}
